﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JCSoft.Collect.Bussness;
using System.Web;

namespace JCSoft.Collect.Web.Controller
{
    public static class WebUtility
    {
        private const string MIDDLE_TIER_KEY = "JCSoft_Collect_MT";

        public static MiddleTier GetMiddleTier()
        {
            HttpContextBase context = new HttpContextWrapper(System.Web.HttpContext.Current);

            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            MiddleTier mt = (MiddleTier)context.Application[MIDDLE_TIER_KEY];

            if (mt == null)
            {
                context.Application.Lock();
                mt = (MiddleTier)context.Application[MIDDLE_TIER_KEY];

                if (mt == null)
                {
                    mt = MiddleTier.Instance;
                    context.Application.Add(MIDDLE_TIER_KEY, mt);
                }

                context.Application.UnLock();
            }

            return mt;
        }
    }
}
